﻿using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;
using MSWord = Microsoft.Office.Interop.Word;
using MSExcel = Microsoft.Office.Interop.Excel;

namespace Core.Helper
{
    public sealed class File
    {
        private static object o = new object();

        public static void Create(string directory, string filename, string content)
        {
            string localdirectory = Helper.Directory.MapPath(directory);
            Helper.Directory.Create(localdirectory);

            string fullpath = localdirectory + filename;

            using (FileStream fs = new FileStream(fullpath, FileMode.Create, FileAccess.Write))
            {
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(content);
                fs.Write(buffer, 0, buffer.Length);
            }
        }

        public static void CreateByBase64String(string base64String, string name, string target)
        {
            byte[] buffer = Convert.FromBase64String(base64String);
            string path = Path.Combine(target, name);
            Helper.Directory.Create(target);
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                fs.Write(buffer, 0, buffer.Length);
            }
        }

        public static void Append(string directory, string filename, string content)
        {
            string localdirectory = Helper.Directory.MapPath(directory);
            Helper.Directory.Create(localdirectory);

            string fullpath = localdirectory + filename;

            using (FileStream fs = new FileStream(fullpath, FileMode.Append, FileAccess.Write))
            {
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(content);
                fs.Write(buffer, 0, buffer.Length);
            }
        }

        public static bool Exists(string path)
        {
            return System.IO.File.Exists(Helper.Directory.MapPath(path));
        }

        public static byte[] Read(string path)
        {
            return System.IO.File.ReadAllBytes(path);
        }

        /// <summary>
        /// Word文档转Pdf
        /// </summary>
        /// <param name="word">Word文档路径</param>
        /// <param name="pdf">生成的Pdf路径</param>
        public static void WordToPdf(string word, string pdf)
        {
            MSWord.Application application = new MSWord.Application();
            MSWord.Document document = null;
            try
            {
                application.Visible = false;
                document = application.Documents.Open(word);
                document.ExportAsFixedFormat(pdf, MSWord.WdExportFormat.wdExportFormatPDF);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (document != null)
                {
                    document.Close();
                }
            }
        }

        /// <summary>
        /// Excel文档转Pdf
        /// </summary>
        /// <param name="excel">Excel文档路径</param>
        /// <param name="pdf">生成的Pdf路径</param>
        public static void ExcelToPdf(string excel, string pdf)
        {
            object missing = Type.Missing;
            MSExcel.Application application = null;
            MSExcel.Workbook workbook = null;

            try
            {
                application = new MSExcel.Application();
                workbook = application.Workbooks.Open(excel, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                if (workbook != null)
                {
                    workbook.ExportAsFixedFormat(
                        MSExcel.XlFixedFormatType.xlTypePDF,
                        pdf,
                        MSExcel.XlFixedFormatQuality.xlQualityStandard,
                        true,
                        false, 
                        missing, missing, missing, missing);
                }
            }
            finally
            {
                if (workbook != null)
                {
                    workbook.Close(true, missing, missing);
                }

                if (application != null)
                {
                    application.Quit();
                }
            }
        }

        public static byte[] Zip(params string[] paths)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (ZipOutputStream zip = new ZipOutputStream(ms))
                {
                    foreach (string path in paths)
                    {
                        string name = Path.GetFileName(path);
                        byte[] buffer = Read(path);
                        ZipEntry entry = new ZipEntry(name);
                        zip.PutNextEntry(entry);
                        zip.Write(buffer, 0, buffer.Length);
                    }

                    return ms.ToArray();
                }
            }
        }
    }
}